package com.xian.im.modules.command.cmd;

import com.xian.im.common.constant.CommandConstant;
import com.xian.im.common.exception.BusinessException;
import com.xian.im.common.model.dto.GroupDto;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.command.Command;
import com.xian.im.modules.command.group.GroupHandlerCommandFactory;
import com.xian.im.modules.im.core.IGroupCoreService;

import java.security.NoSuchAlgorithmException;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-27 09:26
 **/
public class GroupHandlerCommand implements Command {

    /**
     * 执行
     *
     * @param request
     * @param ctxId
     */
    @Override
    public void exec(MsgRequest request, String ctxId) {
        GroupDto entity = SpringContextHolder.getBean(IGroupCoreService.class).getDtoByUniqueId(request.getGroupUniqueId());
        if (entity != null) {
            try {
                GroupHandlerCommandFactory.type(request.getCmdCode()).exec(request,ctxId,entity);
                return;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        throw new BusinessException("群组不存在");
    }

    /**
     * 获取命令
     *
     * @return
     */
    @Override
    public Integer getCmd() {
        return CommandConstant.GROUP_CMD;
    }
}
